
const BScroll = require('better-scroll').default;
const fetch = require('../models/fetch').default;
const productListTpl = require("../views/cate-product-list.html");
const productsTpl = require('../views/cate-products.html');
let page = 0;
let dataObj=[];
let isAll=false;
let bScroll = new BScroll('main', {
    probeType: 1,
    click: true,
    mouseWheel: {
        speed: 20,
        invert: false,
        easeTime: 300
    }
});
$('#products-container').html(productsTpl);
async function getData(cb) {
    console.log(page);
    let productListData = await fetch.getData(`/api/api/getGoods?page=${++page}&zy_ids=p8_c4_l4&app_name=zhe&catname=tab_hpzc&flag=tab_hpzc`);
    let data = productListData.data.goods;
    dataObj=[...dataObj,...data];
    console.log(dataObj.length,productListData.data.new_goods_count)
    if(dataObj.length>=productListData.data.new_goods_count)
    {
        bScroll.off('scroll');
        bScroll.off('scrollEnd');
        $('.foot b').text('没有数据啦~');
        foot.attr('src', '');
    }
    const renderedProductTpl = template.render(productListTpl, { dataObj });
    $('#products').html(renderedProductTpl);
    if(cb) cb();
    bScroll.refresh();
}
getData();
// 初始化位置
bScroll.scrollTo(0, 0);
let foot = $('.foot img');
let bottomImgHasClass = foot.hasClass('down');
bScroll.on('scroll', function () {
    let y = this.y
    let maxY = this.maxScrollY - y

    // // 下拉，当隐藏的loading完全显示的时候触发
    // if (y >= 0) {
    //   !topImgHasClass && head.addClass('up')
    //   return
    // }

    // 上拉，当滚动到最底部时候触发
    if (maxY >= 0) {
        !bottomImgHasClass && $(foot).addClass('down');
        return;
    }

});
bScroll.on('scrollEnd', function () {
    let maxY = this.maxScrollY - this.y
    if (maxY > -40 && maxY < 0) {
        this.scrollTo(0, this.maxScrollY + 40);
        foot.removeClass('down');
    } else if (maxY >= 0) {
        foot.attr('src', '/images/ajax-loader.gif')
        console.log(1)
        getData(function(){
            foot.attr('src', '/images/arrow.png');
        });
    }
})
